package GUI;
import Control.CatchPhrase;
import processing.core.PImage;


class CatchPhraseButton extends Component
{
	////////////////////////////////////////////////////////////////////////////////////////////////////
	public CatchPhraseButton()
	{
		width = 165;
		height = 25;
	}
	public void setLabel(String name)
	{
		label = name;
		
	}
	public void draw()
	{
		Utils.globalProcessing.textFont(Utils.plotFont, 15);
		if(label.length()>Utils.MAX_CATCHPHRASE_LENGTH)
			width = 200;
		else width = (int)(Utils.globalProcessing.textWidth(label)) + 10;
		
		int cx = x;
		int cy = y;

		if(label != null)
		{
			Utils.globalProcessing.textAlign(Utils.globalProcessing.LEFT);

			if(mouseOver()) 
			{
				super.draw();
				if(Utils.globalProcessing.mousePressed)
				{
					
					Utils.gu.drawVGradient(x, y + height, width, -height, Utils.globalProcessing.color(150, 150, 180), 80, Utils.globalProcessing.color(150, 150, 180), 255, (float)0.8);    
					Utils.gu.drawBox(x, y, width, height, 0, Utils.globalProcessing.color(150, 150, 200), 255);
					Utils.gu.drawBox(x, y, width, height, 1, Utils.globalProcessing.color(150, 150, 200), 150);
					
					cx += 2;
					cy += 2;

					
					//add the catchPhrase to the list or remove it if it is already present
					if(Utils.catchPhrasesSelected.contains(c)){
						//Utils.catchPhrasesSelected.remove(c);
						//change catch phrase to not selected or selected
						int i;
						for(i =0;i<Utils.ccip.cd.size();i++){
							if(Utils.ccip.cd.get(i).characterSelectedButtonPanel.c.getName().equals(c.getCharacter().getName())){
								for(int j=0;j<Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.size();j++){
									if(Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).label.equals(c.getPhrase())){
										if(Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).selected==true){
											Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).selected=false;
											Utils.catchPhrasesSelected.remove(Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).catchPhrase);
										}
										else{
											System.out.println("Error mismatch catchPhrasesSelected");
										}
									break;
									}
								}
							}
							
					}
					}
					else{ //if not contained
						Utils.ccip.addCharacterSelected(c.getCharacter());
						Utils.catchPhrasesSelected.add(c);
						int i;
						for(i=0;i<Utils.ccip.cd.size();i++){
							if(Utils.ccip.cd.get(i).characterSelectedButtonPanel.c.getName().equals(c.getCharacter().getName())){
								for(int j=0;j<Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.size();j++)
									if(Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).label.equals(c.getPhrase())){
										Utils.ccip.removeOtherCatchPhrases(this);
										Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).selected=true;
									}
									break;
							}
							}
					}
					Utils.catchPhrasesTextBox.active=false;
					//Utils.catchPhrasesTextBox.text = c.getPhrase(); 
            		Utils.graphArea.createLineGraphPoints();

					
					
				}
				
				if(label.length()>Utils.MAX_CATCHPHRASE_LENGTH)
					drawInfoBox(x-(int)(Utils.globalProcessing.textWidth(label))-20,y,(int)(Utils.globalProcessing.textWidth(label)) + 10,label);

			}

			Utils.globalProcessing.strokeWeight(1);
			Utils.globalProcessing.fill(255);
			if(label.length()>Utils.MAX_CATCHPHRASE_LENGTH)
				Utils.globalProcessing.text(label.substring(0, Utils.MAX_CATCHPHRASE_LENGTH)+"...", cx + 5, cy + 20);
			else
				Utils.globalProcessing.text(label, cx + 5, cy + 20);
			
		}
	}
	
	
	public void drawInfoBox(int x,int y,int width, String label){
	    Utils.globalProcessing.fill(200);
	    Utils.globalProcessing.stroke(80);
	    Utils.globalProcessing.strokeWeight(1);
	    Utils.globalProcessing.rect(x, y, width, 30);

	    Utils.globalProcessing.fill(0);
	    //Utils.globalProcessing.textFont(Utils.globalProcessing.plotFont, 14);
	    Utils.globalProcessing.textAlign(Utils.globalProcessing.LEFT);
	    Utils.globalProcessing.text(label, x + 5, y + 20);
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	PImage icon;
	String label;
	public CatchPhrase c;
}